﻿using ExpenseSystem.DataUpdater;
using ExpenseSystem.DataUpdater.Validation;
using ExpenseSystem.Fether;
using ExpenseSystem.Model;
using System;

namespace ExpenseSystem.Core.CustomerModules
{
    public class CustomerUpdaterRegister
    {
        int rows;
        public NormalDataUpdater<CustomerModel> GetCustomerUpdater()
        {
            NormalDataUpdater<CustomerModel> systemDataUpdater = new NormalDataUpdater<CustomerModel>();
            systemDataUpdater.DataUpdating += DataNotSpaceValid;
            systemDataUpdater.DataUpdating += SingletonValid;
            systemDataUpdater.DataUpdating += DocExist;
            systemDataUpdater.DataDeleting += CheckAvaliable;
            return systemDataUpdater;
        }
        private void CheckAvaliable(object sender, DataValidationEventArgs<CustomerModel> e)
        {
            string message;
            OrderFetcher orderFetcher = new OrderFetcher();
            var record = orderFetcher.ListByCustomerId(e.Delete.Site, e.Delete.AutoId, out message, out rows);
            if (record.Count>0)
            {
                e.Message = String.Format("客户{1}已存在{0}笔订单记录",record.Count,e.Delete.Code);
                e.Cancle = true;
                return;
            }
        }

        private void SingletonValid(object sender, DataValidationEventArgs<CustomerModel> e)
        {
            CustomerFetcher CustomerFetcher = new CustomerFetcher();
            string message = "";
            CustomerModel Customer = CustomerFetcher.GetByCode( e.Insert.Site, e.Insert.Code, out message);
            if (Customer != null && Customer.AutoId != e.Insert.AutoId)
            {
                e.Cancle = true;
                message = String.Format("代号[{0}]已被[{1}]占用", Customer.Code, Customer.Name);
                return;
            }
        }

        private void DataNotSpaceValid(object sender, DataValidationEventArgs<CustomerModel> e)
        {
            if (String.IsNullOrWhiteSpace(e.Insert.Code))
            {
                e.Message = "代号不能为空";
                e.Cancle = true;
                return;
            }
            if (String.IsNullOrWhiteSpace(e.Insert.Name))
            {
                e.Message = "名称不能为空";
                e.Cancle = true;
                return;
            }
            if(String.IsNullOrWhiteSpace(e.Insert.Address))
            {
                e.Message = "地址不能为空";
                e.Cancle = true;
                return;
            }
            if (String.IsNullOrWhiteSpace(e.Insert.Tel))
            {
                e.Message = "电话不能为空";
                e.Cancle = true;
                return;
            }
        }
        private void DocExist(object sender, DataValidationEventArgs<CustomerModel> e)
        {
            string message;
            EmployeeFetcher employeeFetcher = new EmployeeFetcher();
            RegionFetcher regionFetcher = new RegionFetcher();
            SalesPointFetcher salesPointFetcher = new SalesPointFetcher();
            EmployeeModel sales = employeeFetcher.Get(e.Insert.Site, e.Insert.SalesId, out message);
            if(sales==null&&e.Insert.SalesId!=0)
            {
                e.Message = String.Format("业务员信息有误:{0}",message);
                e.Cancle = true;
                return;
            }
            SalesPointModel salesPointModel = salesPointFetcher.Get(e.Insert.Site, e.Insert.SalesPointId, out message);
            if (salesPointModel == null)
            {
                e.Message = String.Format("门店信息有误:{0}", message);
                e.Cancle = true;
                return;
            }
            if (e.Insert.DesignerId > 0)
            {
                EmployeeModel designer = employeeFetcher.Get(e.Insert.Site, e.Insert.DesignerId, out message);
                if (designer == null)
                {
                    e.Message = String.Format("设计师信息有误:{0}", message);
                    e.Cancle = true;
                    return;
                }
            }
        }
    }
}
